package hr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hr.entity.DO.PositionEmployeesDO;
import hr.entity.DO.PositionsDepartmentDO;
import hr.entity.EmployeeOnboardingForm;
import hr.entity.Position;
import hr.entity.vo.PositionEmployeesVo;
import hr.entity.vo.PositionVo;
import hr.exception.CustomException;
import hr.mapper.EmployeeOnboardingFormMapper;
import hr.mapper.PositionMapper;
import hr.service.PositionsService;
import hr.utils.CommonUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 岗位信息表 服务实现类
 * </p>
 *
 * @author zzh
 * @since 2023-12-06
 */
@Service
public class PositionsServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionsService {
    @Autowired
    private PositionMapper positionsMapper;

    @Autowired
    private EmployeeOnboardingFormMapper onboardingFormMapper;

    // 获取岗位列表
    @Override
    public List<PositionsDepartmentDO> selectAllPosition(PositionVo positionVo) {
        long offset = (positionVo.getPage() - 1) * positionVo.getLimit();
        List<PositionsDepartmentDO> positions = positionsMapper.selectAllPosition(positionVo.getPositionName(), positionVo.getPositionStatus(), offset, positionVo.getLimit());
        positions.forEach(e -> {
            e.setCreateTimeStr(CommonUtils.convertTimestampToYMDAndTime(e.getCreateTime()));
        });
        return positions;
    }

    @Override
    public List<Position> getPositionIdAndName() {
        List<Position> positionsList = positionsMapper.getPositionIdAndName();
        return positionsList;
    }

    // 岗位下员工查询
    @Override
    public List<PositionEmployeesDO> getEmployeeByPositionId(PositionEmployeesVo positionEmployeesVO) {
        long offset = (positionEmployeesVO.getPage() - 1) * positionEmployeesVO.getLimit();
        List<PositionEmployeesDO> positions1 = positionsMapper.getEmployeeByPositionId(positionEmployeesVO.getPositionId(), positionEmployeesVO.getName(), offset, positionEmployeesVO.getLimit());
        return positions1;
    }

    // 查询岗位列表总条数
    @Override
    public Integer selectCount(PositionVo positionVo) {
//        long offset = (positionVo.getPage() - 1) * positionVo.getLimit();
        Integer count = positionsMapper.findCount(positionVo.getPositionName(), positionVo.getPositionStatus());
        return count;
    }


    @Override
    public boolean deletePostByPositionId(Integer positionId) throws Exception {
        // 检查岗位状态
        QueryWrapper<Position> wrapper = new QueryWrapper<>();
        wrapper.eq("position_id", positionId)
                .eq("position_status", "1");

        Position positions = positionsMapper.selectOne(wrapper);
        if (positions != null) {
            throw new CustomException("该岗位状态异常，无法删除");
        }

        // 检查岗位下是否有员工信息
        QueryWrapper<EmployeeOnboardingForm> employeeWrapper = new QueryWrapper<>();
        employeeWrapper.eq("position_id", positionId);

        List<EmployeeOnboardingForm> employee = onboardingFormMapper.selectList(employeeWrapper);
        if (employee == null || !employee.isEmpty()) {
            throw new CustomException("该岗位下存在员工信息，无法删除");
        }

        // 进行删除操作
        int deletedRows = positionsMapper.deletePostByProbationId(positionId);
        if (deletedRows > 0) {
            return true;
        } else {
            throw new CustomException("删除操作未成功，请重试");
        }
    }

    @Override
    public boolean addPositions(Position position) {
        position.setCreateTime(System.currentTimeMillis());
        int insert = positionsMapper.insert(position);
        return insert != 0;
    }

    @Override
    public List<Position> getPositionsSalary() {
        List<Position> positionList = positionsMapper.getPositionsSalary();
        return positionList;
    }

    @Override
    public List<Map<String, Object>> getPositionsPeoples() {
        return positionsMapper.selectPositionsPeoples();
    }

    @Override
    public Map<String,Object> handlePositionsSalaryAndPeople() {
        HashMap<String, Object> map = new HashMap<>();
        List<Position> positionList = this.getPositionsSalary();
        List<String> salaries = new ArrayList<>();
        List<String> positions = new ArrayList<>();
        for (Position position : positionList) {
            salaries.add(String.valueOf(position.getSalary()));
            positions.add(position.getPositionName());
        }
        map.put("salaries",salaries);
        map.put("positions",positions);


        List<Map<String, Object>> list = this.getPositionsPeoples();
        ArrayList<Long> peopleList = new ArrayList<>();
        list.forEach(e -> {
            long o = (long)e.get("count");
            peopleList.add(o);
        });
        map.put("peopleList",peopleList);
        return map;
    }

}
